#SI A.2: Regression results----------------
rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_UK_BES.RData")

#Regression output---------------------

#Baseline
vote.UKIP.BES<-list()
vote.UKIP.BES[[1]] <- glm(w6_voteUKIP ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age + Age_missing, data=data_BES, family=binomial)
vote.UKIP.BES[[2]] <- glm(w6_voteUKIP ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES, family=binomial)

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age", "Age missing", "GCSE D-G", "GCSE A-C", "A-level", "Undergraduate", "Postgraduate", "Income", "Income missing", "Political cynicism", "Social conservatism",  "Economic conservatism")
stargazer2(vote.UKIP.BES, odd.ratio = TRUE, title="British Election Study: Support for UKIP", 
           align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), 
           covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"), notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/BES_results.tex", no.space=TRUE, label="tab:BeS_results" )

#calculate 1 SD above and below the mean
low  <- mean(zero1(data_BES$agre), na.rm=T)-sd(zero1(data_BES$agre), na.rm=T)
high <- mean(zero1(data_BES$agre), na.rm=T)+sd(zero1(data_BES$agre), na.rm=T)

#Odds differences
odds.difference <- (exp(vote.UKIP.BES[[2]]$coefficients)[2]*low) / (exp(vote.UKIP.BES[[2]]$coefficients)[2]*high)
1/odds.difference
  
#SI A.2: Correlation matrix ------------------
cor_ivs<-(with(data_BES, data.frame(agre, open,con, ext ,neu, w6_pol_cynicism , w4_immiatt , w6_redistribution)))

#create matrix with complete observations
names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", "6. Cynicism",
                    "7. Social conservatism", "8. Economic conservatism")
 
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
names(correlation.matrix) <- seq(1,8,1)
stargazer(as.matrix(correlation.matrix), title="British Election Studies: Correlation Matrix of Independent Variables", out="Tables/BES_cor.tex", no.space=TRUE, label="tab:BES_cor", digits=2)

#SI A.2: Favorability of political parties------------
like.cons <- lm(zero1(likeCon) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age + Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)
like.lab <- lm(zero1(likeLab) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)
like.libdems <- lm(zero1(likeLD) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)
like.greens <- lm(zero1(likeGrn) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)
like.BNP <- lm(zero1(likeBNP) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)
like.UKIP <- lm(zero1(likeUKIP) ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)

#Make a plot for Agreeableness and Candidate support
favorability <- data.frame(rbind(summary(like.UKIP)$coefficients[c(2:2),1:2], summary(like.cons)$coefficients[c(2:2),1:2], summary(like.lab)$coefficients[c(2:2),1:2], summary(like.libdems)$coefficients[c(2:2),1:2], summary(like.BNP)$coefficients[c(2:2),1:2], summary(like.greens)$coefficients[c(2:2),1:2]))
favorability$candidate <- factor(c("1.UKIP","3.Tories","5.Labour","4.Lib. Dems", "2.BNP", "6. Greens"))
favorability$trait <- c(rep("Agreeableness",6))
colnames(favorability)[2]="se"
fig_BES <- ggplot(favorability ,aes(x=candidate, y=Estimate, colour=candidate))+geom_pointrange(aes(ymin=Estimate-1.96*se,ymax=Estimate+1.96*se), size=1)+ ylim(-0.10,0.15) + theme_minimal()+theme(legend.position="off")+ylab("Party favorability")+xlab("")+geom_hline(yintercept=0)+  theme(axis.text.x=element_text(size=14), strip.text.x=element_text(size=14)) + scale_colour_manual(values=c("red", "red", "blue", "blue", "blue", "blue", "blue")) + facet_grid(.~trait)
ggsave(fig_BES, file="Figures/BES_Agree_fav.pdf", width=6, height=4)


#SI A.2: Controlling for Authoritarianism--------------------
cor.test(data_BES$agre, data_BES$auth_index)

#Regression models controlling for Authoritarianism 
vote.auth<-list()
vote.auth[[1]] <- glm(w6_voteUKIP ~zero1(agre)+ zero1(auth_index) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age + Age_missing, data=data_BES, family=binomial)
vote.auth[[2]] <- glm(w6_voteUKIP ~zero1(agre)+ zero1(auth_index) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism), data=data_BES, family=binomial)
stargazer2(vote.auth, odd.ratio = TRUE, title="British Election Study: Support for UKIP controlling for Authoritarianism", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), covariate.labels = c("Agreeableness", "Authoritarianism", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age", "Age Missing", "GCSE D-G", "GCSE A-C", "A-level", "Undergraduate", "Postgraduate", "Income", "Income missing", "Don't know", "Political cynicism"),dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Full model"),  notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/BES_results_auth.tex", no.space=TRUE, label="tab:BES_results_auth")

#Like of political parties
like.cons.wa <- lm(zero1(likeCon) ~ zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing+ zero1(w6_pol_cynicism), data=data_BES)
like.lab.wa  <- lm(zero1(likeLab) ~zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad +income + income_missing + zero1(w6_pol_cynicism), data=data_BES)
like.libdems.wa  <- lm(zero1(likeLD) ~zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism), data=data_BES)
like.greens.wa  <- lm(zero1(likeGrn) ~zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism), data=data_BES)
like.BNP.wa  <- lm(zero1(likeBNP) ~zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism), data=data_BES)
like.UKIP.wa  <- lm(zero1(likeUKIP) ~zero1(auth_index) +zero1(agre)+ zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism), data=data_BES)

#Make a plot for Agreeableness and Candidate support
favorability <- data.frame(rbind(summary(like.UKIP.wa)$coefficients[c(2:2),1:2], summary(like.cons.wa)$coefficients[c(2:2),1:2], summary(like.lab.wa)$coefficients[c(2:2),1:2], summary(like.libdems.wa)$coefficients[c(2:2),1:2], summary(like.BNP.wa)$coefficients[c(2:2),1:2], summary(like.greens.wa)$coefficients[c(2:2),1:2]))
favorability$candidate <- factor(c("1.UKIP","3.Tories","5.Labour","4.Lib. Dems", "2.BNP", "6. Greens"))
favorability$trait <- c(rep("Authoritarianism",6))
colnames(favorability)[2]="se"
fig_BES_auth <- ggplot(favorability ,aes(x=candidate, y=Estimate, colour=candidate))+geom_pointrange(aes(ymin=Estimate-1.96*se,ymax=Estimate+1.96*se), size=1)+ ylim(-.25,0.3) + theme_minimal()+theme(legend.position="off")+ylab("Party favorability")+xlab("")+geom_hline(yintercept=0)+ theme(axis.text.x=element_text(size=14), strip.text.x=element_text(size=14)) + scale_colour_manual(values=c("red", "red", "blue", "blue", "blue", "blue", "blue")) +ggtitle("")
ggsave(fig_BES_auth, file="Figures/BES_auth.pdf", width=8, height=4)

#SI A.2: Descriptive statistics-----


#Cronbach's alpha Authoritarianism 
psych::alpha(data.frame(data_BES$au1, data_BES$au2, data_BES$au3,data_BES$au4))
#Cronbach's alpha Social conservatism
psych::alpha(data.frame(data_BES$w4_immigCultural_rec, data_BES$w4_immigEcon_rec, data_BES$w4_immigrantsWelfareState))
cor.test(data_BES$w4_immiatt, data_BES$w6_redistribution)
median(data_BES$income_descriptives, na.rm=T)

desc.labels <- c("Populist Vote", labels)
stargazer(vote.UKIP.BES[[2]]$model, covariate.labels = desc.labels,
          type = "latex", summary.stat = c("mean", "sd", "median", "N", "min",  "max"), title="Descriptive statistics British Election Studies sample", out="Tables/BES_descrip.tex", no.space=TRUE, label="tab:BES_descriptives", digits=2)


desc.labels <- c("Populist Vote", labels)
stargazer(vote.UKIP.BES[[2]]$model, covariate.labels = desc.labels,
          type = "latex", summary.stat = c("mean", "sd", "median", "N", "min",  "max"), title="Descriptive statistics British Election Studies sample", out="Tables/BES_descrip.tex", no.space=TRUE, label="tab:BES_descriptives", digits=2)

#descriptives authoritarianism
labels.auth <- c("Populist Vote", "Agreeableness", "Authoritarianism", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age", "Age missing", "GCSE D-G", "GCSE A-C", "A-level", "Undergraduate", "Postgraduate", "Income", "Income missing", "Political cynicism", "Social conservatism",  "Economic conservatism")
stargazer(vote.auth[[2]]$model, covariate.labels = labels.auth, type = "latex", summary.stat = c("mean", "sd", "median", "N", "min",  "max"), title="Descriptive statistics British Election Studies sample", out="Tables/BES_descrip_auth.tex", no.space=TRUE, label="tab:BES_descriptives", digits=2)

